package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.pojo.*;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.AddressService;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Reference(retries = 0,timeout = 10000)
    private OrderService orderService;

    @Reference
    private AddressService addressService;

    @Reference
    private CartService cartService;

    @RequestMapping("toOrderShow")
    public String toOrderShow(){
        return "home-index";
    }


    @RequestMapping("toGetOrderInfo")
    public String toGetOrderInfo(Model model){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        List<TbAddress> addressList = addressService.getAddressListByUser(tbUser.getUsername());
        for (TbAddress tbAddress : addressList) {
            String province = addressService.getProvinceById(tbAddress.getProvinceId());
            tbAddress.setProvince(province);
            String city = addressService.getCityById(tbAddress.getCityId());
            tbAddress.setCity(city);
            String town = addressService.getAreaById(tbAddress.getTownId());
            tbAddress.setTown(town);
        }
        model.addAttribute("addressList",addressList);

        List<TbProvinces> provincesList = addressService.getProvincesList();
        model.addAttribute("provincesList",provincesList);
        return "getOrderInfo";
    }

    @RequestMapping("insertOrder")
    @ResponseBody
    public Result insertOrder(TbOrder tbOrder, Long addressId,String cartList){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            List<Cart> carts = JSON.parseArray(cartList, Cart.class);
            TbAddress addressById = addressService.getAddressById(addressId);
            tbOrder.setUserId(tbUser.getUsername());
            Map<String,Object> map = orderService.insertOrder(tbOrder,addressById,carts);
            return Result.jsonResponse(ResultEnum.OK).setData(map);
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("listOrderByUser")
    @ResponseBody
    public Result listOrderByUser(Integer pageNum, Integer pageSize) {
        // 获取当前登录的用户
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        try {
            PageInfo<TbOrder> pageInfo = orderService.listOrderByUser(pageNum, pageSize, user);
            return Result.jsonResponse(ResultEnum.OK).setData(pageInfo);
        } catch (Exception e) {
            logger.error("错误", e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
